home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_400 / 428_02 / libsrc / kbdedit.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-03-13  |  3.7 KB  |  180 lines

  1. /*
  2. ** kbdedit.c
  3. **
  4. ** Pictor, Version 1.51, Copyright (c) 1992-94 SoftCircuits
  5. ** Redistributed by permission.
  6. */
  7.  
  8. #include <ctype.h>
  9. #include <string.h>
  10. #include "pictor.h"
  11.  
  12. #define UPDATE_NOUPDATE 0
  13. #define UPDATE_CURSOR    1
  14. #define UPDATE_EDIT        2
  15.  
  16.  
  17. int _PL_editfill = '\xFA';
  18. int _PL_tabendedit = FALSE;
  19.  
  20. #define MAX_STRLEN  128
  21. static char localbuff[MAX_STRLEN + 1];
  22.  
  23. /*
  24. ** Allows the user to edit the given string. Initially, the string
  25. ** is selected. If the user types new characters, they replace the
  26. ** selected text. If the user presses an edit key (left, right,
  27. ** backspace, etc), the original string is edited. Returns TRUE if
  28. ** the user pressed Enter, returns FALSE and buffer is unchanged if
  29. ** the user pressed escape
  30. */
  31. int kbdedit(char *buffer,int row,int col,int width,
  32.     int maxlen,COLORSTRUCT *colors)
  33. {
  34.     int i,key,pos,len,retval = 0;
  35.     int leftcol = 0,update = UPDATE_EDIT,done = FALSE,first_time = TRUE;
  36.  
  37.     strcpy(localbuff,buffer);
  38.     pos = len = strlen(localbuff);
  39.  
  40.     if(maxlen > MAX_STRLEN)
  41.         maxlen = MAX_STRLEN;
  42.  
  43.     pushcurs();
  44.     showcurs(TRUE);
  45.  
  46.     while(!done) {
  47.         if(update != UPDATE_NOUPDATE) {
  48.             if(pos <= leftcol) {
  49.                 leftcol = (pos > 0) ? (pos - 1) : pos;
  50.                 update = UPDATE_EDIT;
  51.             }
  52.             else if(pos >= (leftcol + width)) {
  53.                 leftcol = ((pos - width) + 1);
  54.                 update = UPDATE_EDIT;
  55.             }
  56.             if(update == UPDATE_EDIT) {
  57.                 setvpos(row,col);
  58.                 for(i = 0;i < width;i++) {
  59.                     if((leftcol + i) < len) {
  60.                         vcolor((first_time) ? colors->select : colors->normal);
  61.                         vputc(localbuff[i + leftcol]);
  62.                     }
  63.                     else {
  64.                         vcolor(colors->normal);
  65.                         vputc((char)_PL_editfill);
  66.                     }
  67.                 }
  68.             }
  69.             setcurs(row,col + (pos - leftcol));
  70.             update = UPDATE_NOUPDATE;
  71.         }
  72.         switch(key = kbdread()) {
  73.             case HOME_KEY:
  74.                 if(pos > 0) {
  75.                     pos = 0;
  76.                     update = UPDATE_CURSOR;
  77.                 }
  78.                 break;
  79.             case END_KEY:
  80.                 if(pos < len) {
  81.                     pos = len;
  82.                     update = UPDATE_CURSOR;
  83.                 }
  84.                 break;
  85.             case LEFT_KEY:
  86.                 if(pos > 0) {
  87.                     pos--;
  88.                     update = UPDATE_CURSOR;
  89.                 }
  90.                 else beep();
  91.                 break;
  92.             case RIGHT_KEY:
  93.                 if(pos < len) {
  94.                     pos++;
  95.                     update = UPDATE_CURSOR;
  96.                 }
  97.                 else beep();
  98.                 break;
  99.             case BACKSPACE_KEY:
  100.                 if(pos > 0) {
  101.                     pos--;
  102.                     memmove(localbuff+pos,localbuff+pos+1,len-pos);
  103.                     len--;
  104.                     update = UPDATE_EDIT;
  105.                 }
  106.                 else beep();
  107.                 break;
  108.             case DELETE_KEY:
  109.                 if(pos < len) {
  110.                     memmove(localbuff+pos,localbuff+pos+1,len-pos);
  111.                     len--;
  112.                     update = UPDATE_EDIT;
  113.                 }
  114.                 else beep();
  115.                 break;
  116.             case TAB_KEY:
  117.                 if(_PL_tabendedit) {
  118.                     strcpy(buffer,localbuff);
  119.                     retval = -1;
  120.                     done = TRUE;
  121.                 }
  122.                 else beep();
  123.                 break;
  124.             case SHIFTTAB_KEY:
  125.                 if(_PL_tabendedit) {
  126.                     strcpy(buffer,localbuff);
  127.                     retval = -2;
  128.                     done = TRUE;
  129.                 }
  130.                 else beep();
  131.                 break;
  132.             case ESCAPE_KEY:
  133.                 retval = FALSE;
  134.                 done = TRUE;
  135.                 break;
  136.             case ENTER_KEY:
  137.                 strcpy(buffer,localbuff);
  138.                 retval = TRUE;
  139.                 done = TRUE;
  140.                 break;
  141.             case F1_KEY:
  142.                 if(_PL_helpfunc != NULL)
  143.                     _PL_helpfunc(_PL_helpcontext);
  144.                 else
  145.                     beep();
  146.                 continue;
  147.             default:
  148.                 key = key & 0xFF;
  149.                 if(isprint(key)) {
  150.                     if(first_time) { /* typing replaces selected text */
  151.                         leftcol = pos = len = 0;
  152.                         localbuff[0] = '\0';
  153.                     }
  154.                     if(len < maxlen) {
  155.                         len++;
  156.                         memmove(localbuff+pos+1,localbuff+pos,len - pos);
  157.                         localbuff[pos] = (char)key;
  158.                         pos++;
  159.                         update = UPDATE_EDIT;
  160.                     }
  161.                     else beep();
  162.                 }
  163.                 else {
  164.                     beep();
  165.                     continue;
  166.                 }
  167.                 break;
  168.         }
  169.         if(first_time) {
  170.             first_time = FALSE;
  171.             setvpos(row,col);
  172.             vrepa(colors->normal,width);
  173.         }
  174.     }
  175.     popcurs();
  176.  
  177.     return(retval);
  178.  
  179. } /* kbdedit */
  180.